//
//  RootItemCell.m
//  MyFunctionDemo
//
//  Created by Ting Han on 2017/9/21.
//  Copyright © 2017年 Ting Han. All rights reserved.
//

#import "RootItemCell.h"
#import "Item.h"
#import "UIView+AnimationProperty.h"

@interface RootItemCell ()

@property (nonatomic,strong) UILabel *titleLabel;
@property (nonatomic,strong) UILabel *subtitleLabel;

@end
@implementation RootItemCell

- (void)setupCell{
    self.accessoryType  = UITableViewCellAccessoryDisclosureIndicator;
    self.selectionStyle = UITableViewCellSelectionStyleNone;
}
- (void)buildSubview{
    
    self.titleLabel      = [[UILabel alloc] initWithFrame:CGRectMake(10, 8, 290, 25)];
    [self addSubview:self.titleLabel];
    
    self.subtitleLabel           = [[UILabel alloc] initWithFrame:CGRectMake(10, 35, 290, 10)];
    self.subtitleLabel.font      = [UIFont systemFontOfSize:8.f];
    self.subtitleLabel.textColor = [UIColor grayColor];
    [self addSubview:self.subtitleLabel];
    
}
- (void)loadContent{
    if (self.dataAdapter.data) {
        
        Item *item                     = self.dataAdapter.data;
        self.titleLabel.attributedText = item.attrName;
        self.subtitleLabel.text        = [NSString stringWithFormat:@"%@", [item.data class]];
    }
    
    if (self.indexPath.row % 2) {
        
        self.backgroundColor = [[UIColor grayColor] colorWithAlphaComponent:0.05f];
        
    } else {
        
        self.backgroundColor = [UIColor whiteColor];
    }
    
}
- (void)selectedEvent{
    Item *item = self.dataAdapter.data;
    [self.viewController.navigationController pushViewController:[[item.data class]new] animated:YES];
}

- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated{
    [super setHighlighted:highlighted animated:animated];
    if (self.highlighted) {
        CABasicAnimation *scaleAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
        scaleAnimation.duration = 0.1f;
        scaleAnimation.fromValue = [NSValue valueWithCGPoint:CGPointMake(1, 1)];
        scaleAnimation.toValue = [NSValue valueWithCGPoint:CGPointMake(0.9, 0.9)];
        [self.titleLabel.layer addAnimation:scaleAnimation forKey:@"scaleAnimation"];
    }else{
        
        [UIView animateWithDuration:0.3 delay:0.f usingSpringWithDamping:10 initialSpringVelocity:2 options:UIViewAnimationOptionCurveEaseInOut animations:^{
            self.titleLabel.scale = 1;
        } completion:^(BOOL finished) {
            
        }];
        
//        CASpringAnimation *springAnimation = [CASpringAnimation animationWithKeyPath:@"transform.scale"];
//        springAnimation.toValue             = [NSValue valueWithCGPoint:CGPointMake(1, 1)];
//        springAnimation.initialVelocity = 2.f;
//        springAnimation.damping = 10;
//        springAnimation.mass = 100;
//        springAnimation.stiffness = 90;
//        [self.titleLabel.layer addAnimation:springAnimation forKey:@"sprAnimation"];
    }
}

@end
